var app = new Vue({
	router,
	el: '#app',
	store,
	data() {
		return {
			storyid: '', //故事id
			ftabledata: [], //项目数据
			roledata: [], //角色数据
			lodingstoryid: null,
			ShowHidden:true,
			//公告信息
			Notice: "欢迎参加故事家（仿）安安专属版v1.0.0的模块α测试，注册系统正在开发中，参加测试请找管理员申请账号。后台管理系统正在开发中，任何bug和使用问题请联系开发人员QQ：378984514。",
			token: '', //token令牌
			signdialog: true, //是否启用登录窗口，不启用请为token赋合法值
			//数据验证方法
			rules: {
				name: [{
						min: 0,
						max: 30,
						message: '最长30个字符',
						trigger: 'change'
					},
					{
						pattern: "^[A-Za-z0-9]+$",
						message: '只能输入英文和数字',
						trigger: 'change'
					},
				],
				pass: [{
						min: 0,
						max: 30,
						message: '最长30个字符',
						trigger: 'change'
					},
					{
						pattern: "^[A-Za-z0-9]+$",
						message: '只能输入英文和数字',
						trigger: 'change'
					},
				]
			},
			form: {
				name: "", //账户
				pass: "", //密码
			},
		}
	},
	//应用方法
	methods: {
		/*
		 * 项目模块开关
		 */
		Project_switch() {
			document.getElementById('rolechart').src = ``;
			document.getElementById('etp').style.display = "none";
			document.getElementById('etpx').style.display = "block";
			this.$refs.so.$el.style.display = "block";
			this.$refs.rm.$el.style.display = "none";
		},
		/*
		 * 角色模块开关
		 */
		Role_switch() {
			document.getElementById('rolechart').src = ``;
			document.getElementById('etp').style.display = "none";
			document.getElementById('etpx').style.display = "none";
			this.$refs.so.$el.style.display = "none";
			this.$refs.rm.$el.style.display = "block";
			document.getElementById('rmx').style.display = "block";
			document.getElementById('role_details').style.display = "block";
			document.getElementById('newrole').style.display = "none";
			document.getElementById('addrelationship').style.display = "none";
			document.getElementById('editrole').style.display = "none";
			document.getElementById('addattribute').style.display = "none";
			document.getElementById('editrelationship').style.display = "none";
		},
		EventSwitch() {
			if (this.lodingstoryid != this.$store.state.storyid) {
				this.lodingstoryid = this.$store.state.storyid;
				document.getElementById('rolechart').src = ``;
				this.$refs.ro.$el.style.display = "none";
				this.$refs.so.$el.style.display = "none";
				this.$refs.rm.$el.style.display = "none";
				axios.get(
						`./api/Controller2.php?ac=EventTableloding&storyid=${this.$store.state.storyid}&token=${this.$store.state.token}`
						)
					.then(response => {
						this.$store.commit('updataToken', response.data[1]);
						this.$store.commit('updataEventTable', response.data[0]);
					})
					.catch(function(error) {
						console.log(error);
					})
			} else {
				document.getElementById('rolechart').src = ``;
				this.$refs.ro.$el.style.display = "none";
				this.$refs.so.$el.style.display = "none";
				this.$refs.rm.$el.style.display = "none";
			}
		},
		/*
		 * 组件开关
		 * 注：临时使用，有机会要进行修复！
		 */
		ComponentSwitch() {
			document.getElementById('rolechart').src = ``;
			this.$refs.ro.$el.style.display = "none";
			this.$refs.so.$el.style.display = "none";
			this.$refs.rm.$el.style.display = "none";
		},
		/*
			初始化数据
			登录账户获取token
		*/
		sign_in() {
			//过滤非法字符
			var name = this.form.name.replace(/^[A-Za-z0-9]+$/g, "");
			var pass = this.form.pass.replace(/^[A-Za-z0-9]+$/g, "");
			/*
			 * 进行校验
			 * 成功获取token并进行初始化
			 * 失败提示错误信息
			 */
			if (name === "" && pass === "") {
				axios.get('./api/Controller.php?ac=sig&name=' + this.form.name + '&pass=' + this.form.pass + '')
					.then(response => {
						//申请token令牌
						if (response.data != "" && response.data != false) {
							this.token = response.data
							//登录
							axios.get('./api/Controller.php?ac=loding&name=' + this.form.name + '&pass=' +
									this.form.pass + '&token=' + this.token + '')
								.then(response => {
									//登录成功关闭登录窗口
									this.signdialog = false
									//账户初始化
									axios.get('./api/Controller.php?ac=BS&token=' + this.token + '')
										.then(response => {
											console.log(response)
											this.token = response.data.pop()
											this.ftabledata = this.ftabledata.concat(response.data)
										})
										.catch(function(error) {
											console.log(error);
										});
								}) //发生错误时抛出
								.catch(function(error) {
									console.log(error);
								});
						} else {
							this.$message.error('密码或账户错误！');
						}
					})
					.catch(function(error) {
						console.log(error);
					});
			} else {
				this.$message.error('密码或账户错误！');
			}
		},
		//想不起来干什么的了，删了不能正常显示角色数据
		receivestoryid(data) {
			this.storyid = data
		},
		testbutton2() {
			console.log(this)
		},
		notloding(done) {
			return false
		},
		handleSelect(key, keyPath) {
			console.log(key, keyPath);
		}
	},
	watch: {
		//监听故事id，发生变化重新加角色数据
		storyid: function(newval) {
			this.storyid = newval
			axios.get('./api/Controller.php/?ac=roleloding&token=' + this.token + '&storyid=' + this
					.storyid + '')
				.then(response => {
					this.roledata = []
					this.roledata = this.roledata.concat(response.data[0])
					this.token = response.data.token
				})
				.catch(function(error) {
					console.log(error)
				})
		},
		//监听token，发生变化更新store中的数据
		token: function(newval) {
			this.$store.commit('updataToken', newval);
		}
	},
	//应用初始化
	created: function() {
		if (this.$route.path !== "/Project_Components") {
			this.$router.push({
				path: '/Project_Components'
			})
		}
		//公告
		/* this.$alert(this.Notice,'公告', {
				  center: true,
		          confirmButtonText: '确定',
		            }); */
	},
})
